package com.example.locationreport.locationreport.http.network;

import android.text.TextUtils;

import com.example.locationreport.locationreport.bean.CommonBean;
import com.example.locationreport.locationreport.global.Constant;
import com.example.locationreport.locationreport.utils.JsonUtils;
import com.example.locationreport.locationreport.utils.LogLocationUtils;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * @author : YanKing
 * @date : 2019/6/11  15:38
 * @Description : 网络回调
 */
public abstract class OkGoCallBackListener extends StringCallback {

    private OkGoBackListener listener;

    public OkGoCallBackListener(OkGoBackListener listener) {
        this.listener = listener;
    }

    @Override
    public void onSuccess(Response<String> response) {

        if (response != null && !TextUtils.isEmpty(response.body())) {
            CommonBean commonBean = JsonUtils.parseJsonToBean(response.body(), CommonBean.class);

            if(commonBean != null) {
                if (commonBean.getCode() == 0) {
                    onSuccess(response.body());
                } else {
                    listener.onWrong(commonBean.getMsg());
                }
            }else {
                listener.onChangeError("转换CommonBean转换失败");
            }
        } else {
            listener.onDataEmpty();
        }
    }

    @Override
    public void onError(Response<String> response) {
        super.onError(response);
        listener.onError(response.message());
        listener.onError();
    }

    public abstract void onSuccess(String body);
}
